/*
 * @description: 分页请求
 * @fileName: useListLoadClass.ts
 * @author: lxx
 * @date: 2023-07-08 08:55:52
 * @version: V1.0.0
 */
import { ref } from 'vue'

class PaginationLoader<T> {
    // 分页数据
    private data = ref<T[]>([])

    // 分页基础参数
    private requestParams = {
        page: 1,
        limit: 10
    }

    // 是否正在加载
    private isLoading = ref(false)

    // 是否还有更多数据
    private hasMore = ref(true)

    // 请求函数（由外部传入）
    private fetchFn: (formData: Object, onSuccess: Function) => Function

    constructor(fetchFn: (formData: Object, onSuccess: Function) => Function) {
        this.fetchFn = fetchFn
    }

    // 获取当前数据
    get dataList() {
        return this.data.value
    }

    // 获取是否正在加载
    get loading() {
        return this.isLoading.value
    }

    // 获取是否还有更多数据
    get more() {
        return this.hasMore.value
    }

    // 加载下一页数据
    async loadNextPage() {
        // if (this.isLoading.value || !this.hasMore.value) return

        // uni.showLoading({ title: '加载中...' })

        // this.isLoading.value = true

        // try {
        //     const result = await this.fetchFn(this.currentPage.value, this.pageSize)

        //     if (result.length > 0) {
        //         this.data.value = [...this.data.value, ...result]
        //         this.currentPage.value += 1
        //     } else {
        //         this.hasMore.value = false // 没有更多数据
        //     }
        // } catch (error) {
        //     console.error('加载数据失败:', error)
        // } finally {
        //     this.isLoading.value = false
        // }
        uni.showLoading({ title: '数据加载中' })
        this.isLoading.value = false
        const params = { ...this.requestParams } as { [n: string]: string | number | boolean }

        setTimeout(() => {
            this.fetchFn(params, ({ data }: { data: { data: any; total: number } }) => {
                this.isLoading.value = true
                this.requestParams.page = params.page as number
                this.requestParams.limit = params.limit as number
                this.data.value = [...this.data.value, ...data.data]
                uni.hideLoading()
            })
        }, 100)
    }

    // 重置分页状态
    reset() {
        this.data.value = []
        this.requestParams.page = 1
        this.hasMore.value = true
        this.isLoading.value = false
    }
}

// export default PaginationLoader

const fetchData = async (formData: any, onSuccess: Function) => {
    const submitData = { ...formData }
    point_order_list(submitData).then((res) => {
        const { data } = res as { data: { data: any; total: number } }
        onSuccess({ data })
    })
    // // 模拟接口请求
    // return new Promise<{ id: number; name: string }[]>((resolve) => {
    //     setTimeout(() => {
    //         const data = Array.from({ length: pageSize }, (_, index) => ({
    //             id: (page - 1) * pageSize + index,
    //             name: `Item ${(page - 1) * pageSize + index}`
    //         }))
    //         resolve(data)
    //     }, 1000)
    // })
}

// 创建分页加载实例
const paginationLoader = new PaginationLoader(fetchData)

// 加载下一页数据
paginationLoader.loadNextPage(formDate.value)

// 初始化加载第一页数据
paginationLoader.loadNextPage()

return {
    dataList: paginationLoader.dataList,
    loading: paginationLoader.loading,
    more: paginationLoader.more,
    loadNextPage
}
